package com.panshi.service;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class SendSmsService {

    public void sendsms(String code,String phone){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GB8eRGequgBseZ4ZZwf", "RO9WLZ6DorRIkwly5ipPcP0LVM4v8G");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        //验证码手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //网址公司名称
        request.putQueryParameter("SignName", "xhlMr博客");
        //模版CODE
        request.putQueryParameter("TemplateCode", "SMS_197890040");
        //发送的验证码
        HashMap<String,Object> map = new HashMap<>();

        //随机生成6位验证码

        //保存到redis

        map.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
